/**
 *  Copyright (C) 2013  Piotr Szczepa�ski
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package edu.pw.elka.gtsna.centrality.degree;

import edu.pw.elka.gtsna.centrality.NodeCentrality;
import edu.pw.elka.gtsna.community_detection.CentralityForCommunityStructure;
import edu.pw.elka.gtsna.community_detection.CommunityStructure;
import edu.pw.elka.gtsna.graph.Graph;

/**
 * @author Piotr Lech Szczepa�ski
 * @author P.Szczepanski@ii.pw.edu.pl 
 *
 */
public class OVDegreeCentrality extends NodeCentrality implements CentralityForCommunityStructure{

	CommunityStructure cs;
	
	
	public OVDegreeCentrality(Graph graph, CommunityStructure partition) {
		super(graph);
		this.cs = partition;
	}

	@Override
	public void computeCentrality() {
		for(int node: graph.getAllNodes()){
			int Cj = cs.getNodeCommunityNumber(node);
			double value = 0.0;
			for(int neighbour: graph.getNeigbours(node)){
				// if node and neighbour are from the same community
				if (cs.getNodesCommunity(neighbour).contains(node)) {
					value+= 1.0/( (1.0 + graph.getInterCommunityDegree(neighbour, cs))*
								  (1.0 + graph.getIntraCommunityDegree(neighbour,cs,Cj)));
				} else {
					value+= 1.0/( (1.0 + graph.getInterCommunityDegree(neighbour, cs))*
							  (graph.getIntraCommunityDegree(neighbour,cs,Cj)));
				}
			}
			value+= (1.0-( (1.0+graph.getInterCommunityDegree(node, cs))*
					  (1.0+graph.getIntraCommunityDegree(node, cs, Cj)))) /
					  ( (1.0 + graph.getInterCommunityDegree(node, cs))*
							  (1.0+graph.getIntraCommunityDegree(node,cs ,Cj )));
			
    		centralities.put(node, value) ;
    	}
	}


	@Override
	public CommunityStructure getCommunityStructure() {
		return cs;
	}

	@Override
	public void setCommunityStructure(CommunityStructure cs) {
		this.cs = cs;
	}

}
